package com.bawei.library_common.utils

import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import java.lang.ref.WeakReference


object DialogUtills {
    private var weakReference:WeakReference<Context>? = null
    /**
     * 显示确认对话框
     * @param context 上下文
     * @param title 标题
     * @param msg 显示文本
     * @param agreeTxt 确认文本
     * @param unagreeTxt 取消文本
     * @param agree 确认回调
     * @param close 关闭回调
     */
    fun showConfirmDialog(
        context:Context,
        title:String,
        msg:String,
        agreeTxt:String,
        unagreeTxt: String,
        agree:DialogInterface.OnClickListener,
        close:DialogInterface.OnClickListener){
        //为什么弱引用？？？防止activity内存泄漏？？ 当页面销毁的时候，对话框没destory，由于对话框持有activity的引用会导致activity无法被回收
        weakReference=WeakReference(context)
        val builder: AlertDialog.Builder? = weakReference?.get()?.let { AlertDialog.Builder(it) };
        builder?.setTitle(title)
        builder?.setMessage(msg)
        builder?.setPositiveButton(agreeTxt,agree)
        builder?.setNegativeButton(unagreeTxt,close)
        builder?.show()
    }

    /**
     * 关闭对话框
     */
    fun dismiss(){
        weakReference?.clear()
        weakReference = null
    }
}